<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of insertSurat
 *
 * @author brlnt
 */
class Dashboard extends CI_Controller {
	
	public function Dashboard(){
		parent::__construct();
		if(!$this->session->userdata('islogin')) redirect('auth');
		$this->load->model('surat_model');
	}

    public function index() {
		/*$hasil = $this->surat_model->getNotifSurat();
        // menentukan hasil query ada atau tidak
        if ($hasil->num_rows() > 0) {
            $data['hasil'] = $hasil;
        } else {
            show_404();
            //$data['message'] = 'Tidak ada notifikasi';
        }*/
        // load view
		
		
		//var_dump($data);
		$data ['jumlah_notif'] = $this->surat_model->getNotifSurat()->num_rows();
		$data['title'] = 'Beranda';
        $this->load->view('dashboard_view', $data);
    }

    /* Fungsi untuk menerima request dari ajax,
     * yang menampilkan notifikasi
     */
    public function notifikasi(){
    	$this->load->library('pagination');
        $query = $this->surat_model->getNotifSurat();

        $config['base_url'] = site_url('dashboard/notifikasi/');
        $config['total_rows'] = $query->num_rows();
        $config['per_page'] = '4';
        $config['first_page'] = 'Awal'; 
		$config['last_page'] = 'Akhir'; 
		$config['next_page'] = '&laquo;'; 
		$config['prev_page'] = '&raquo;';
		$config['uri_segment'] = 3;
		//styling pagination
		$config['full_tag_open'] = '<ul class="pagination">';
		$config['full_tag_close'] = '</ul>';
		$config['first_tag_open'] = '<li>';
		$config['first_tag_close'] = '</li>';
		$config['last_tag_open'] = '<li>';
		$config['last_tag_close'] = '</li>';
		$config['next_tag_open'] = '<li>';
		$config['next_tag_close'] = '</li>';
		$config['prev_tag_open'] = '<li>';
		$config['prev_tag_close'] = '</li>';
		$config['cur_tag_open'] = '<li class="active"><span>';
		$config['cur_tag_close'] = '<span class="sr-only">(current)</span></span></li>';
		$config['num_tag_open'] = '<li>';
		$config['num_tag_close'] = '</li>';

		$page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
        //initiation
        $this->pagination->initialize($config);
        $data['pagination'] = $this->pagination->create_links();

        if ($query->num_rows() > 0){
        	$results = $this->surat_model->getNotifSurat($page, $config['per_page']);
            $data['results'] = $results->result();
            //$data['hasil'] = $query;
		} else {
            $data['message'] = 'Tidak ada data tersimpan pada kategori ini.';
        }

        $json_output = null;
		//check if ajax is called
		if($this->input->post('ajax', FALSE)){
			$json_output = json_encode(array(
				'results' => $data["results"],
				'pagination' => $data['pagination']
			));
		}

		$this->output
			->set_header("header('Access-Control-Allow-Origin:*')")
			->set_content_type('application/json')
			->set_output($json_output);
    }
}

?>
